<?php

namespace app\modules\setting\controllers\fsjl;

use app\models\EtcSendCreatorDetail;
use Yii;
use app\modules\setting\models\fsjl\EtcSendCreatorDayLog;
use app\modules\setting\configs\fsjl\YjrwlbConfig;
use app\modules\setting\models\fsjl\EtcSendCreatorDayLogSearch;
use sookie\react\AdminController;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Exception;
use sookie\excel\SaveExcel;
use sookie\db\DBHandler;


/**
 * YjrwlbController implements the CRUD actions for EtcSendCreatorDayLog model.
 */
class YjrwlbController extends AdminController
{
    /**
    * @return array
    * 列表数据
    */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcSendCreatorDayLogSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $item = $item->toArray();
                    $success = EtcSendCreatorDetail::find()->andWhere(['status'=>1])->andWhere(['log_id' =>$item['id']])->count();
                    $faled = EtcSendCreatorDetail::find()->andWhere(['status'=>-1])->andWhere(['log_id' =>$item['id']])->count();
                    $item['num'] = "<span style='color: green'>{$success}</span>/<span style='color: red'>{$faled}</span>";
                    $day = date("Ymd",strtotime($item['day']));
                    $item['job_id'] = "{$day}-{$item['group_id']}{$item['job_id']}";
                    return $item;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],YjrwlbConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
    * @param $id
    * @return array
    * 详情接口
    */
    public function actionView($id)
    {
        try {
            return ArrayHelper::merge(["data" => EtcSendCreatorDayLog::findOne($id)],YjrwlbConfig::configView());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }





    protected function findModel($id)
    {
        if (($model = EtcSendCreatorDayLog::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }




}

